package model;

import java.text.DecimalFormat;
import java.util.UUID;

import common.Enums.MethodType;

public class QuantitativeValue extends Value
{
	private double result;
	private static String digitFormat = "##";
	
	public QuantitativeValue()
	{
		super(MethodType.QUANTITATIVE);
		result = 1;
	}
	
	public QuantitativeValue(Double result, UUID ID)
	{
		super(MethodType.QUANTITATIVE, ID);
		setResult(result);
	}
	
	public QuantitativeValue(Double result)
	{
		super(MethodType.QUANTITATIVE);
		setResult(result);
	}

	public double getResult() 
	{
        return result;
	}
	
	public void setResult(Double result) 
	{
		DecimalFormat decimalForm = new DecimalFormat("#." + digitFormat);
		this.result = Double.valueOf(decimalForm.format(result).replace(",","."));
		super.setStringResult(result.toString());
	}
	
	/**
	 * Method sets the pattern for showing the result with some specific quantity of decimal numbers.<br>
	 * Example: For the number 20.21082 to be shown as 20.21, you should call the method with "##" as parameter : 'setDigitFormat("##");'
	 * @param newDigitFormat The numbers (in a # sequence form) of decimal to be shown. 
	 */
	public void setDigitFormat(String newDigitFormat)
	{
		digitFormat = newDigitFormat;
	}
	
	@Override
	public void setStringResult(String newResult) 
	{
		try
		{
			result = Double.parseDouble(newResult);
			super.setStringResult(newResult);
		}
		catch(NumberFormatException e)
		{
			//Leave the value the way it is and don't updated it
		}
	}
	
	@Override
	public String getStringResult() {
		return String.valueOf(result);
	}
	
	public static QuantitativeValue parseQuantitativeValue(Value val)
	{
		QuantitativeValue retVal = new QuantitativeValue(new Double(1), val.getID());
		
		retVal.setCreatedDate(val.getCreatedDate());
		retVal.setHasError(false);
		retVal.setLastUpdated(val.getLastUpdated());
		retVal.setReliability(val.getReliability());
		retVal.setSourceType(val.getSourceType());
		retVal.setSource(val.getSource());
		retVal.setUnit(val.getUnit());
		
		return retVal;
	}
}
